@echo off
echo ========================================
echo 性能测试
echo ========================================

echo 等待服务启动...
timeout /t 10 /nobreak >nobreak >nul

echo.
echo ========================================
echo 1. 并发用户测试
echo ========================================

echo 测试10个并发用户注册...
for /l %%i in (1,1,10) do (
    start /b curl -s -X POST http://localhost:8080/api/users/register -H "Content-Type: application/json" -d "{\"username\":\"user%%i\",\"email\":\"user%%i@example.com\",\"password\":\"password123\"}" >nul
)
timeout /t 5 /nobreak >nul
echo 并发注册测试完成

echo.
echo 测试10个并发课程查询...
for /l %%i in (1,1,10) do (
    start /b curl -s -X GET http://localhost:8080/api/courses >nul
)
timeout /t 3 /nobreak >nul
echo 并发查询测试完成

echo.
echo ========================================
echo 2. 响应时间测试
echo ========================================

echo 测试API网关响应时间...
curl -w "API网关响应时间: %%{time_total}s\n" -o nul -s http://localhost:8080/actuator/health

echo 测试用户服务响应时间...
curl -w "用户服务响应时间: %%{time_total}s\n" -o nul -s http://localhost:8082/actuator/health

echo 测试课程服务响应时间...
curl -w "课程服务响应时间: %%{time_total}s\n" -o nul -s http://localhost:8083/actuator/health

echo 测试练习服务响应时间...
curl -w "练习服务响应时间: %%{time_total}s\n" -o nul -s http://localhost:8085/actuator/health

echo 测试进度服务响应时间...
curl -w "进度服务响应时间: %%{time_total}s\n" -o nul -s http://localhost:8084/actuator/health

echo.
echo ========================================
echo 3. 负载测试
echo ========================================

echo 进行100次连续请求测试...
set /a success=0
set /a total=0

for /l %%i in (1,1,100) do (
    curl -s -X GET http://localhost:8080/api/courses >nul
    if !errorlevel! equ 0 (
        set /a success+=1
    )
    set /a total+=1
)

echo 成功请求: !success!/!total!
set /a success_rate=!success!*100/!total!
echo 成功率: !success_rate!%%

echo.
echo ========================================
echo 4. 资源使用监控
echo ========================================

echo 查看容器资源使用情况...
docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"

echo.
echo ========================================
echo 性能测试完成！
echo ========================================

echo 📋 性能指标说明:
echo - 响应时间 < 1s: 优秀
echo - 响应时间 1-3s: 良好
echo - 响应时间 > 3s: 需要优化
echo - 成功率 > 95%%: 优秀
echo - 成功率 90-95%%: 良好
echo - 成功率 < 90%%: 需要优化
echo.

echo 🔍 查看详细性能日志:
echo docker-compose logs --tail=100 api-gateway
echo docker-compose logs --tail=100 user-management-service
echo.

pause
